﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guia3.Models;

using Raven.Client.Document;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Linq;
//using Raven.Client.Shard;
//using Raven.Storage.Esent;

namespace Guia3.Controllers
{
    public class NoticiaController : Controller
    {
        // get
        public ActionResult List()
        {
            //var db = HttpContext.Application["db"] as DocumentStore;// EmbeddableDocumentStore;
            //using (var session = db.OpenSession()) //*** abre a sessão para trabalhar com o banco de dados
            using (var session = MvcApplication.DocDB.OpenSession())
            {
                var Noticias = from noticia in session.Query<Noticia>()
                            select noticia;
                return View(Noticias);

            }
        }

        //
        // GET: /Noticia/
        public ActionResult New() //abertura inicial do formulario.
        {
            Noticia not = new Noticia { Id = 0, Titulo = "", Resenha = "", Texto = "", Foto = "", Fonte = "" };
            return View("Edit", not);
        }

        public ActionResult ToEdit(int Id) //localiza e mostra registro pra edição
        {
            using (IDocumentSession session = MvcApplication.DocDB.OpenSession())
            {
                var results = (from noticia in session.Query<Noticia>()
                               where noticia.Id == Id
                               select noticia).ToArray();
                return View("Edit", results[0]);
            }
        }

        [HttpPost] // ****  Este bloco recebe os dados do formulário e Grava....
        public ActionResult Save(FormCollection campos)
        {

            using (IDocumentSession session = MvcApplication.DocDB.OpenSession())
            {
                Noticia Not = session.Load<Noticia>(int.Parse(campos["Id"]));
                if (Not == null)
                {
                    Not = new Noticia
                    {
                        Id = int.Parse(campos["Id"]),
                        Titulo = campos["Titulo"],
                        Resenha = campos["Resenha"],
                        Texto = campos["Texto"],
                        Foto = campos["Foto"],
                        Fonte = campos["Fonte"]
                    };
                    session.Store(Not);
                }
                else
                {
                    Not.Titulo = campos["Titulo"];
                    Not.Resenha = campos["Resenha"];
                    Not.Texto = campos["Texto"];
                    Not.Foto = campos["Foto"];
                    Not.Fonte = campos["Fonte"];
                }
                session.SaveChanges();
            }

            return RedirectToAction("List", "Noticia");
        }

        public ActionResult Delete(int Id)
        {
            using (var session = MvcApplication.DocDB.OpenSession())
            {
                Noticia Not = session.Load<Noticia>(Id);
                session.Delete(Not);

                session.SaveChanges();
            }
            return RedirectToAction("List", "Noticia");
        }

    }
}

